package testivrs.testivrs;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Database_access {

    private static Connection con = null;
    private static Statement statement = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultSet = null;

    public Database_access() throws Exception {
        // This will load the MySQL driver, each DB has its own driver
        Class.forName("com.mysql.jdbc.Driver");
        // Setup the connection with the DB
        con = DriverManager.getConnection("jdbc:mysql://localhost/ivrs", "root", "");
    }

    public int login(String name, String pass) throws Exception {
        //     public static void main(String args[]) throws Exception {

        
        statement = con.createStatement();
        
        resultSet = statement.executeQuery("select * from ivrs.Table_Studentdata where Col_username='" + name + "' and Col_pin ='" + pass + "' ");

       

        resultSet.last();
        int temp = resultSet.getRow();

        //System.out.println(temp);

        if (temp == 1) {
            return 1;
        }

        resultSet = statement.executeQuery("select * from ivrs.Table_Facultydata where Col_username='" + name + "' and Col_password ='" + pass + "' ");
        resultSet.last();
        temp = resultSet.getRow();
        if (temp == 1) {
            return 2;
        }

        return 0;







    }

    public int StudentReg(String name, String mobno, int pin, String rollno) throws Exception {

        statement = con.createStatement();




        statement.executeUpdate("INSERT INTO Table_Studentdata (Col_username, Col_studentname, Col_mobileNo, Col_pin, Col_rollno) VALUES ('sanchit@iiitd.ac.in', '" + name + "', '" + mobno + "', '" + pin + "', '" + rollno + "')");
        return 0;


    }

    public int FacultyReg(String email, String name, String password, String mobno)
            throws Exception {
        statement = con.createStatement();




        statement.executeUpdate("INSERT INTO Table_Facultydata (Col_username, Col_Facultyname, Col_password, Col_Mobileno) VALUES ('" + email + "', '" + name + "', '" + password + "', '" + mobno + "')");
        return 0;

    }
}
